package org.hhh.user.service.impl;

import org.hhh.basic.constant.BaseConstants;
import org.hhh.basic.exception.BusinessException;
import org.hhh.basic.service.impl.BaseServiceImpl;
import org.hhh.basic.util.MD5Utils;
import org.hhh.basic.util.StrUtils;
import org.hhh.org.mapper.EmployeeMapper;
import org.hhh.user.domain.Logininfo;
import org.hhh.user.domain.User;
import org.hhh.user.dto.UserDto;
import org.hhh.user.mapper.LogininfoMapper;
import org.hhh.user.mapper.UserMapper;
import org.hhh.user.service.IUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private LogininfoMapper logininfoMapper;

    @Override
    public void phoneRegister(UserDto userDto) {
        //1.校验
        //1.1校验不能为空
        if (StringUtils.isEmpty(userDto.getPhone())||StringUtils.isEmpty(userDto.getVerifyCode())||
        StringUtils.isEmpty(userDto.getPassword())||StringUtils.isEmpty(userDto.getConfirmPassword())){
            throw new BusinessException("不能为空！！！");
        }
        //1.2校验两次密码是否一致
        if (!userDto.getPassword().equals(userDto.getConfirmPassword())){
            throw new BusinessException("两次密码不一致！！！");
        }
        //1.3校验用户名是否存在
        User userName = userMapper.loadByPhone(userDto.getPhone());
        if (userName != null) {
            throw new BusinessException("用户名已经被注册！！！");
        }
        //2.校验验证码
        //2.1校验验证码是否过期
        Object codeTmp = redisTemplate.opsForValue().get(BaseConstants.BUSINESS_REGISTER_PREFIX + userDto.getPhone());
        if (codeTmp == null) {
            throw new BusinessException("验证码已经过期了！！！");
        }
        //2.2验证码是否填写正确
        String code=((String) codeTmp).split(":")[0];
        if (!userDto.getVerifyCode().equalsIgnoreCase(code)){
            throw new BusinessException("请填写正确的验证码！！！");
        }
        //3.保存
        User user=userDto2User(userDto);
        Logininfo logininfo=User2Logininfo(user);
        //保存logininfo
        logininfoMapper.save(logininfo);
        //保存user
        user.setLogininfo_id(logininfo.getId());
        userMapper.save(user);


    }

    private User userDto2User(UserDto userDto) {
        User user = new User();
        user.setUsername(userDto.getPhone());
        user.setPhone(userDto.getPhone());
        String salt = StrUtils.getComplexRandomString(32);
        //使用盐值加密
        String md5 = MD5Utils.encrypByMd5(userDto.getPassword() + salt);
        user.setSalt(salt);
        user.setPassword(md5);
        return user;
    }

    private Logininfo User2Logininfo(User user) {
        Logininfo logininfo = new Logininfo();
        //同名拷贝
        BeanUtils.copyProperties(user,logininfo );
        logininfo.setType(1);
        return logininfo;
    }
}
